package com.test.demo;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * ${DESCRIPTION}
 *
 * @author muxiaoyu
 * @create 2017/05/16 10:37
 **/
public class RunTimeProxy implements InvocationHandler {
    private Object obj;

    public Object bind(Object obj) {
        this.obj = obj;
        return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object result;
        long startTime = System.nanoTime();
        result = method.invoke(obj, args);
        long endTime = System.nanoTime();
        System.out.println("程序运行时间：" + (endTime - startTime) + "ns");
        return result;
    }
}
